package com.sicau.smarttraceplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.smarttraceplatform.entity.CollectingData;
import com.sicau.smarttraceplatform.entity.param.collectingdata.CollectingDataQueryParam;
import com.sicau.smarttraceplatform.entity.vo.PageDataVO;
import com.sicau.smarttraceplatform.entity.vo.collectingdata.CollectingDataVO;
import com.sicau.smarttraceplatform.entity.vo.trace.TraceInfoVO;
import com.sicau.smarttraceplatform.mapper.CollectingDataMapper;
import com.sicau.smarttraceplatform.mapper.vo.CollectingDataVOMapper;
import com.sicau.smarttraceplatform.mapper.vo.TraceInfoVOMapper;
import com.sicau.smarttraceplatform.service.CollectingDataService;
import com.sicau.smarttraceplatform.utils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 采集数据 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2020-05-24
 */
@Service
public class CollectingDataServiceImpl extends ServiceImpl<CollectingDataMapper, CollectingData> implements CollectingDataService {

    @Resource
    private TraceInfoVOMapper traceInfoVOMapper;

    @Resource
    private CollectingDataVOMapper collectingDataVOMapper;


    @Override
    public CollectingDataVO queryCollectingDataInfosByCondition(CollectingDataQueryParam queryParam) {

        //查询溯源信息
        LambdaQueryWrapper<TraceInfoVO> traceInfoVOLambdaQueryWrapper = new LambdaQueryWrapper<TraceInfoVO>()
                .eq(TraceInfoVO::getTraceId, queryParam.getTraceId());
        TraceInfoVO traceInfoVO = traceInfoVOMapper.selectOne(traceInfoVOLambdaQueryWrapper);
        CollectingDataVO.TraceInfo traceInfo = new CollectingDataVO.TraceInfo();
        BeanUtils.copyProperties(traceInfoVO, traceInfo);

        //查询数据采集详情
        LambdaQueryWrapper<CollectingDataVO.CollectingDataInfo> dataLambdaQueryWrapper = new LambdaQueryWrapper<CollectingDataVO.CollectingDataInfo>();
        LocalDateTime collectionStartTime = queryParam.getCollectionStartTime();
        LocalDateTime collectionEndTime = queryParam.getCollectionEndTime();
        if (collectionStartTime != null) {
            dataLambdaQueryWrapper.gt(CollectingDataVO.CollectingDataInfo::getCollectionTime, collectionStartTime.minusSeconds(1));
        }
        if (collectionEndTime != null) {
            dataLambdaQueryWrapper.lt(CollectingDataVO.CollectingDataInfo::getCollectionTime, collectionEndTime.plusSeconds(1));
        }
        dataLambdaQueryWrapper.eq(!StringUtils.isEmpty(queryParam.getTraceId()), CollectingDataVO.CollectingDataInfo::getTraceId, queryParam.getTraceId());
        Page<CollectingDataVO.CollectingDataInfo> infoVOPage = new Page<>(queryParam.getCurrentPage(), queryParam.getPageSize());
        Page<CollectingDataVO.CollectingDataInfo> collectingDataPage = collectingDataVOMapper.selectPage(infoVOPage, dataLambdaQueryWrapper);
        PageDataVO<CollectingDataVO.CollectingDataInfo> dataPageDataVO = PageUtil.convert(collectingDataPage);
        return new CollectingDataVO(traceInfo, dataPageDataVO);
    }
}
